package com.dangdang.service.impl;

import com.dangdang.dao.BookDao;
import com.dangdang.entity.Book;
import com.dangdang.service.BookService;
import com.dangdang.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class BookServiceImpl implements BookService {
    @Override
    public List<Book> selectByChoice(String orderby, int count) {
        SqlSession sqlSession = null;
        List<Book> books = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            BookDao dao = sqlSession.getMapper(BookDao.class);
            books = dao.selectByChoice(orderby, count);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return books;
    }

    @Override
    public Book showOne(int id) {
        SqlSession sqlSession = null;
        Book book = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            BookDao dao = sqlSession.getMapper(BookDao.class);
            book = dao.showOne(id);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return book;
    }

    @Override
    public PageInfo<Book> selectByFid(int fid,int pageNo,int pageSize) {
        SqlSession sqlSession = null;
        PageInfo<Book> pageInfo = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            BookDao dao = sqlSession.getMapper(BookDao.class);
            //设置分页信息。第一个参数是页号，第二个参数是一页显示的条数
            PageHelper.startPage(pageNo,pageSize);
            //根据一级类别查询
            List<Book> books = dao.selectByFid(fid);
            //将查询到的book集合封装到pageInfo对象中
            pageInfo = new PageInfo<>(books);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Book> selectBySid(int sid,int pageNo,int pageSize) {
        SqlSession sqlSession = null;
        PageInfo<Book> pageInfo = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            BookDao dao = sqlSession.getMapper(BookDao.class);
            //设置分页信息。第一个参数是页号，第二个参数是一页显示的条数
            PageHelper.startPage(pageNo,pageSize);
            //根据一级类别查询
            List<Book> books = dao.selectBySid(sid);
            //将查询到的book集合封装到pageInfo对象中
            pageInfo = new PageInfo<>(books);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return pageInfo;
    }
}
